package love.yuanmengrobot.als.system.dao.impl;

/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:HuangHongTao
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/6/18 21:20
 */

import love.yuanmengrobot.als.system.dao.AccountManagementDao;
import love.yuanmengrobot.als.system.javaBean.AlsUser;
import love.yuanmengrobot.als.utilPublic.JdbcUtil;
import love.yuanmengrobot.als.utilPublic.MD5;
import love.yuanmengrobot.als.utilPublic.SqlString;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class AccountManagementDaoImpl implements AccountManagementDao {

    @Override
    public List<AlsUser> allUsers() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<AlsUser> allUsers= new ArrayList<AlsUser>();
        try {
            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();

             rs = stmt.executeQuery(SqlString.getAlsusers());
            while (rs.next()) {
                AlsUser us = new AlsUser();
                us.setUserid(rs.getString("USERID"));
                us.setUserphone(rs.getString("USERPHONE"));
                us.setUsername(rs.getString("USERNAME"));
                us.setUserpassword(rs.getString("USERPASSWORD"));
                us.setUseremail(rs.getString("USEREMAIL"));
                allUsers.add(us);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {//回收资源
            JdbcUtil.close(rs, stmt, conn);
        }

        return allUsers;
    }

    @Override
    public boolean addUser(AlsUser newUser) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();
            int s = stmt.executeUpdate(SqlString.addAlsuser(newUser.getUserphone(),newUser.getUsername(),MD5.encoderByMd5(newUser.getUserpassword()),newUser.getUseremail()));
            if (s == 1) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } finally {//回收资源
            JdbcUtil.close(rs, stmt, conn);
        }
        return false;
    }

    @Override
    public boolean deleteUser(String uid) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();

            int s = stmt.executeUpdate(SqlString.deleteAlsuser(uid));
            if (s == 1) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {//回收资源
            JdbcUtil.close(rs, stmt, conn);
        }
        return false;
    }

    @Override
    public boolean updatePassword(String uid,String newPassword) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();
            int s = stmt.executeUpdate(SqlString.updatePassword(uid,MD5.encoderByMd5(newPassword)));
            if (s == 1) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } finally {//回收资源
            JdbcUtil.close(rs, stmt, conn);
        }
        return false;
    }
}
